<template>

  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div :class="{ 'title-box': obj.cover.type === 1 }">
          <!-- 标题 -->
          <span>{{ obj.title }}</span>
          <div :class="{ 'thumb-box': obj.cover.type > 1 }">
            <van-image
              class="thumb"
              :src="item"
              alt=""
              v-for="(item, index) in obj.cover.images"
              :key="index"
            >
              <template v-slot:loading>
                <van-loading type="spinner" size="20" />
              </template>
            </van-image>
          </div>
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ obj.aut_name }}</span>
            <span>{{ obj.comm_count }}评论</span>
            <span>{{ getPubdate }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="feedbackFn" v-if="isShow"/>
          <!-- 反馈弹出层 动作面板组件-->
          <van-action-sheet
            v-model="feedbackShow"
            :actions="actions"
            @select="onSelect"
            @cancel="onCancle"
            get-container="body"
            :cancel-text="cancelText"
          />
        </div>
      </template>
    </van-cell>
  </div>
</template>

<script>
import { timeAgo } from '@/utils/date'
import { Notify } from 'vant'
import { dislikesAPI, reportsAPI } from '@/api'
import { firstActions, secondActions } from '@/api/report'
export default {
  name: 'articleItem',
  props: ['obj', 'isShow'],
  data () {
    return {
      getPubdate: timeAgo(this.obj.pubdate),
      feedbackShow: false,
      actions: [{ name: '不感兴趣' }, { name: '反馈垃圾内容' }],
      cancelText: '取消'
    }
  },
  methods: {
    feedbackFn () {
      this.feedbackShow = true
    },
    onSelect (action, index) {
      if (action.name === '不感兴趣') {
        this.dislikeFn()
      } else if (action.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.cancelText = '返回'
      } else {
        this.reportsFn(action)
      }
    },
    // 点击退出触发的事件
    onCancle () {
      if (this.cancelText === '返回') {
        this.feedbackShow = true
        this.actions = firstActions
        this.cancelText = '取消'
      }
    },
    async dislikeFn () {
      try {
        await dislikesAPI(this.obj.art_id)
        Notify({ type: 'success', message: '反馈成功' })
        // this.$emit('changeList')
        this.feedbackShow = false
      } catch (err) {
        Notify({ type: 'error', message: '反馈失败' })
        this.feedbackShow = false
      }
    },
    async reportsFn (action) {
      try {
        const data = {
          target: this.obj.art_id,
          type: action.value,
          remark: '就是想举报！'
        }
        await reportsAPI(data)
        Notify({ type: 'success', message: '举报成功' })
        // this.$emit('changeList')
        this.feedbackShow = false
      } catch (err) {
        Notify({ type: 'error', message: '举报失败' })
        this.feedbackShow = false
      }
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
